const express = require('express')
const router = express.Router()
const { verifyToken } = require('../../middilWare/verifyToken')
const {
  createEvent,
  getAllEvents,
  getEventById,
  updateEvent,
  deleteEvent,
} = require('../../services/eventService')

// 创建事件
router.post('/', verifyToken, async (req, res) => {
  try {
    const event = await createEvent(req.body)
    res.status(200).json({ code: 200, message: '事件创建成功', data: event })
  } catch (error) {
    res.status(500).json({ code: 500, message: '事件创建失败' })
  }
})

// 获取所有事件
router.get('/', verifyToken, async (req, res) => {
  try {
    const events = await getAllEvents()
    res.status(200).json({ code: 200, data: events })
  } catch (error) {
    res.status(500).json({ code: 500, message: '获取事件失败' })
  }
})

// 获取单个事件
router.get('/:id', verifyToken, async (req, res) => {
  try {
    const event = await getEventById(req.params.id)
    res.status(200).json({ code: 200, data: event })
  } catch (error) {
    res.status(500).json({ code: 500, message: '获取事件失败' })
  }
})

// 更新事件
router.put('/:id', verifyToken, async (req, res) => {
  try {
    const event = await updateEvent(req.params.id, req.body)
    res.status(200).json({ code: 200, message: '事件更新成功', data: event })
  } catch (error) {
    res.status(500).json({ code: 500, message: '事件更新失败' })
  }
})

// 删除事件
router.delete('/:id', verifyToken, async (req, res) => {
  try {
    await deleteEvent(req.params.id)
    res.status(200).json({ code: 200, message: '事件删除成功' })
  } catch (error) {
    res.status(500).json({ code: 500, message: '事件删除失败' })
  }
})

module.exports = router
